﻿using System.Text;
using System.Net;
using System.Threading;
namespace System.Web.Hosting
{
    internal interface IWebHostRuntime
    {
        void StartProcessing();
        void StopProcessing();
        void ProcessRequestNow(WebHostWorkerRequest wr, NetHttpApplicationFactory applicationFactory);
        NetHttpModuleCollection Modules { get; }
    }

    internal class WebHostRuntime : IWebHostRuntime
    {
        private WebHost _webHost;

        public WebHostRuntime(WebHost webHost)
        {
            if (webHost == null)
                throw new ArgumentNullException("webHost");
            _webHost = webHost;
        }

        public HttpListenerWorkerRequest CreateWorkerRequest(HttpListenerContext netContext)
        {
            return new WebHostWorkerRequest(this, netContext);
        }

        public void ProcessRequestNow(WebHostWorkerRequest wr, NetHttpApplicationFactory applicationFactory)
        {
            //try
            //{
            NetHttpRuntime.ProcessRequestNow(_webHost, wr, applicationFactory);
            //}
            //catch (Exception exception)
            //{
            //    try { NetHttpRuntime.RaiseRuntimeError(exception, this); }
            //    catch { }
            //    if (HttpListenerHost.UseThreading)
            //    {
            //        if (exception is ThreadAbortException)
            //            Thread.ResetAbort();
            //        return;
            //    }
            //    throw;
            //}
        }

        public void StartProcessing() { }

        public void StopProcessing() { }

        public NetHttpModuleCollection Modules
        {
            get { return _webHost.Modules; }
        }
    }
}